<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class GroupAssistantLog extends Model
{
    protected $table = "c_group_assistant_logs";

    public function group_assistant()
    {
        return $this->hasOne('App\Models\GroupAssistant', 'id', 'group_assistant_id');
    }

    public function book()
    {
        return $this->hasOne('App\Models\AddressBook', 'id', 'book_id');
    }



    /***
     * @param $filter
     * @return mixed
     * 获取群发记录
     */
    public function getList($filter)
    {
        $res = $this->where([['work_id', '=', $filter['work_id']],['type','=',$filter['type']]])->with(['group_assistant','book'])->orderBy("id","desc")->simplePaginate(15)->toArray();
        $host = new Url();
        if(empty($res['data'])){
            return $res;
        }
        foreach($res['data'] as $k=>&$v){
            $v['group_assistant']['sound'] = !empty($v['group_assistant']['sound']) ? $host::getHttpHost() . 'upload/' . $v['group_assistant']['sound'] : "";
            $v['group_assistant']['video'] = !empty($v['group_assistant']['video']) ? $host::getHttpHost() . 'upload/' . $v['group_assistant']['video'] : "";
            if (!empty($v['group_assistant']['images'])) {
                foreach ($v['group_assistant']['images'] as $kk => &$vv) {
                    $vv = $host::getHttpHost() . 'upload/' . $vv;
                }
            }
            //获取标签
            $v['book']["label_name"] = (new AddressBookRelateLabel())
                ->join("c_address_book_labels as label","label.id",'=','c_address_book_relate_labels.address_book_label_id')
                ->select("label.label_name")
                ->where([["c_address_book_relate_labels.address_book_id","=",$v['book_id']]])->get();
        }
        return $res;
    }

    /***
     * @param $filter
     * @return array
     * 获取通讯录中病人自动随访
     */
    public function getUserAutoPlanLog($filter)
    {
        $where = [];
        if (isset($filter['type'])) {
            $where[] = ['afp.type', '=', $filter['type']];
        }
        $result = $this->join("c_auto_follow_plans as afp", "afp.id", "=", "c_group_assistant_logs.group_assistant_id")
            ->select("afp.*")
            ->where([["c_group_assistant_logs.book_id", '=', $filter['book_id']], ['c_group_assistant_logs.type', '=', 3],['afp.work_id','=',$filter['work_id']]])
            ->where($where)->get();
        foreach ($result as $k => &$v) {
            $v['mes'] = (new PlanMessage())->where([['auto_follow_plan_id', '=', $v->id]])->orderByDesc("days")->first();
        }
        if ($result->isEmpty()) {
            return [];
        } else {
            $data['data'] = $result->toArray();
            $data['statistics'] = $this->getNumbers($filter);
            return $data;
        }
    }

    public function getNumbers($filter)
    {
        $wait = $this->join("c_auto_follow_plans as afp", "afp.id", "=", "c_group_assistant_logs.group_assistant_id")
            ->where([["c_group_assistant_logs.book_id", '=', $filter['book_id']], ['c_group_assistant_logs.type', '=', 3], ['afp.work_id', '=', $filter['work_id']], ["afp.type", '=', 0]])
            ->count("afp.id");
        $doing = $this->join("c_auto_follow_plans as afp", "afp.id", "=", "c_group_assistant_logs.group_assistant_id")
            ->where([["c_group_assistant_logs.book_id", '=', $filter['book_id']], ['c_group_assistant_logs.type', '=', 3], ['afp.work_id', '=', $filter['work_id']], ["afp.type", '=', 1]])
            ->count("afp.id");
        $end = $this->join("c_auto_follow_plans as afp", "afp.id", "=", "c_group_assistant_logs.group_assistant_id")
            ->where([["c_group_assistant_logs.book_id", '=', $filter['book_id']], ['c_group_assistant_logs.type', '=', 3], ['afp.work_id', '=', $filter['work_id']], ["afp.type", '=', 2]])
            ->count("afp.id");
        return [
            'wait' => $wait,
            'doing' => $doing,
            'end' => $end,
        ];
    }


}
